﻿using System.Collections;
using System.Collections.Generic;

namespace CXW.Core
{
    /// <summary>
    /// Creates tool bar items from a location in the addin tree.
    /// </summary>
    /// <attribute name="label" use="optional">
    /// Label of the tool bar item.
    /// </attribute>
    /// <attribute name="icon" use="optional">
    /// Icon of the tool bar item.
    /// </attribute>
    /// <attribute name="type" use="optional" enum="Separator;CheckBox;Item;ComboBox;DropDownButton">
    /// This attribute must be one of these values:
    /// Separator, CheckBox, Item, ComboBox, DropDownButton
    /// </attribute>
    /// <attribute name="loadclasslazy" use="optional">
    /// Only for the type "Item". When set to false, the command class is loaded
    /// immediately instead of the usual lazy-loading.
    /// </attribute>
    /// <attribute name="tooltip" use="optional">
    /// Tooltip of the tool bar item.
    /// </attribute>
    /// <attribute name="class">
    /// Command class that is run when item is clicked; or class that manages
    /// the ComboBox/DropDownButton. Required for everything except "Separator".
    /// </attribute>
    /// <attribute name="shortcut" use="optional">
    /// Shortcut that activates the command (e.g. "Control|S").
    /// </attribute>
    /// <usage>Any toolbar strip paths, e.g. /SharpDevelop/Workbench/ToolBar</usage>
    /// <children childTypes="MenuItem">A drop down button has menu items as sub elements.</children>
    /// <returns>
    /// A ToolStrip* object, depending on the type attribute.
    /// </returns>
    /// <conditions>Conditions are handled by the item, "Exclude" maps to "Visible = false", "Disable" to "Enabled = false"</conditions>
    public class ToolbarItemDoozer : IDoozer
    {
        /// <summary>
        /// Gets if the doozer handles codon conditions on its own.
        /// If this property return false, the item is excluded when the condition is not met.
        /// </summary>
        public bool HandleConditions
        {
            get { return true; }
        }

        public object BuildItem(BuildItemArgs args)
        {
            return new ToolbarItemDescriptor(args.Caller, args.Codon, args.BuildSubItems<object>(), args.Conditions);
        }
    }

    /// <summary>
    /// Represents a toolbar item. These objects are created by the ToolbarItemDoozer and
    /// then converted into GUI-toolkit-specific objects by the ToolbarService.
    /// </summary>
    public sealed class ToolbarItemDescriptor : IItemDescriptor
    {
        object _caller; Codon _codon;
        IEnumerable<ICondition> _conditions;
        IList _subItem;
        public object Caller { get { return _caller; } }
        public Codon Codon { get { return _codon; } }
        public IEnumerable<ICondition> Conditions { get { return _conditions; } }
        /// <summary>子对象集合</summary>
        public IList SubItems { get { return _subItem; } }

        /// <summary>构造函数</summary>
        /// <param name="caller"></param>
        /// <param name="codon"></param>
        /// <param name="subItems"></param>
        /// <param name="conditions"></param>
        public ToolbarItemDescriptor(object caller, Codon codon, IList subItems, IEnumerable<ICondition> conditions)
        {
            _caller = caller;
            _codon = codon;
            _conditions = conditions;
            _subItem = subItems;
        }
    }
}
